import * as THREE from "three";
import TWEEN from "@tweenjs/tween.js";
/**
 * 获取模型的尺寸信息
 * @param {THREE.Object3D} object - 要测量的Three.js对象
 * @returns {Object} 包含尺寸信息的对象
 */
export function getModelSize(object) {
  // 创建一个边界框对象
  const box = new THREE.Box3();

  // 计算对象的边界框
  box.setFromObject(object);

  // 计算尺寸
  const size = new THREE.Vector3();
  box.getSize(size);

  // 获取中心点
  const center = new THREE.Vector3();
  box.getCenter(center);

  // 返回尺寸信息
  return {
    width: size.x,
    height: size.y,
    depth: size.z,
    min: box.min,
    max: box.max,
    center: center,
    box: box
  };
}

// 计算适合所有模型的相机位置（不应用）
function calculateCameraFitPosition(models, camera) {
  if (models.length === 0) return null;

  // 创建一个空的边界框
  const box = new THREE.Box3();
  // 合并所有模型的边界框
  models.forEach((item) => {
    const modelBox = new THREE.Box3().setFromObject(item);
    box.union(modelBox);
  });

  const size = box.getSize(new THREE.Vector3());
  const center = box.getCenter(new THREE.Vector3());

  // 计算适合显示所有模型的距离
  const maxDim = Math.max(size.x, size.y, size.z);
  const fov = camera.fov * (Math.PI / 180);
  let cameraZ = Math.abs(maxDim / 2 / Math.tan(fov / 2));

  // 添加一些边距
  cameraZ *= 1.8;

  return {
    position: new THREE.Vector3(center.x, center.y, cameraZ),
    target: center.clone(),
    near: box.min.distanceTo(box.max) / 1000,
    far: box.min.distanceTo(box.max) * 1000,
    maxDistance: cameraZ * 5,
    minDistance: cameraZ * 0.5
  };
}

// 根据对象大小调整相机位置
export function adjustCameraToFitObject(models, camera, controls) {
  const fitPosition = calculateCameraFitPosition(models, camera);
  if (!fitPosition) return;

  // 使用 TWEEN 动画调整相机
  const currentPosition = {
    x: camera.position.x,
    y: camera.position.y,
    z: camera.position.z
  };

  const targetPosition = {
    x: fitPosition.position.x,
    y: fitPosition.position.y,
    z: fitPosition.position.z
  };

  // 禁用控制器，防止干扰动画
  controls.enabled = false;

  // 创建位置动画
  new TWEEN.Tween(currentPosition)
    .to(targetPosition, 1500) // 1.5秒动画
    .easing(TWEEN.Easing.Quadratic.InOut)
    .onUpdate(() => {
      camera.position.set(currentPosition.x, currentPosition.y, currentPosition.z);
    })
    .onComplete(() => {
      // 动画完成后启用控制器
      controls.enabled = true;
      controls.saveState();
    })
    .start();

  // 创建目标点动画
  const currentTarget = {
    x: controls.target.x,
    y: controls.target.y,
    z: controls.target.z
  };

  const targetTarget = {
    x: fitPosition.target.x,
    y: fitPosition.target.y,
    z: fitPosition.target.z
  };

  new TWEEN.Tween(currentTarget)
    .to(targetTarget, 1500)
    .easing(TWEEN.Easing.Quadratic.InOut)
    .onUpdate(() => {
      controls.target.set(currentTarget.x, currentTarget.y, currentTarget.z);
    })
    .start();

  // 更新相机的近平面和远平面
  camera.near = fitPosition.near;
  camera.far = fitPosition.far;
  camera.updateProjectionMatrix();

  // 更新控制器的限制
  controls.maxDistance = fitPosition.maxDistance;
  controls.minDistance = fitPosition.minDistance;

  // 应用控制器更改
  controls.update();
}
